package com.example.jack.community.view.home;


import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.jack.community.BaseActivity;
import com.example.jack.community.MainActivity;
import com.example.jack.community.R;
import com.example.jack.community.common.utils.CustomizeScrollView;
import com.example.jack.community.common.utils.HotspotRecyclerView;
import com.example.jack.community.data.HomeCommentBean;
import com.example.jack.community.data.HomeItemBean1;
import com.example.jack.community.model.IHomeComment;
import com.example.jack.community.persenter.HomeCommentpersenter;
import com.example.jack.community.view.home.homeAdapter.HomeDynamicDetailsAdapter;
import com.example.jack.community.view.home.homeAdapter.HomeItemRcAdapter;
import com.example.jack.community.view.home.homeAdapter.NiceEvenType;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;

import de.hdodenhof.circleimageview.CircleImageView;

import static android.webkit.WebSettings.PluginState.ON;
import static com.example.jack.community.R.id.textView;


public class HomeDynamicDetailsActivity extends BaseActivity implements IHomeComment {
    private LinearLayout liearlayout_back;
    private CustomizeScrollView mScrollView;
    private HotspotRecyclerView mRcComment;//每条评论
    private RecyclerView mCVRcPhoto;
    private CircleImageView mCVtx;
    private TextView mCVusername,mCVtime,mCVcontext;
    private TextView nice_tv,browse_tv,comment_tv;
    private ImageView nice_iv;
    private LinearLayout nicelayout;
    private HomeDynamicDetailsAdapter mDetailsAdapter;
    private HomeItemRcAdapter mItemRcAdapter;
    private HomeItemBean1 mBean1;
    private boolean mFlag;
    //首页评论实现类
    private HomeCommentpersenter mCommentpersenter;
    private int mPosition;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home_dynamic_details1);
        //获取首页动态中的数据
        initIntentData();
        //初始化控件
        initView();
        //监听
        initLinstener();

    }



    private void initLinstener() {
        liearlayout_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
                //发送事件，返回时
                EventBus.getDefault().post(new NiceEvenType(mPosition,mBean1));
            }
        });
        browse_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(HomeDynamicDetailsActivity.this, ""+mPosition, Toast.LENGTH_SHORT).show();
            }
        });
        mItemRcAdapter.setOnRcItemListener(new HomeItemRcAdapter.OnRcItemListener() {
            @Override
            public void onRcItemClick(View view, ArrayList<String> urls, int posistion) {
                 new HomeItemImagesDialog(view.getContext(),urls,posistion).show();
            }
        });
        final boolean niceFlag = mBean1.getNiceFlag();
        if(niceFlag){
            nice_iv.setImageResource(R.drawable.ic_heard_true4);
        }else{
            nice_iv.setImageResource(R.drawable.ic_heard_false4);
        }
        //点赞逻辑，待优化
        nicelayout.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               if(mBean1.getNiceFlag()){
                  nice_iv.setImageResource(R.drawable.ic_heard_false4);
                  mBean1.setNiceFlag(false);
                   String s = nice_tv.getText().toString();
                   int i = Integer.parseInt(s);  i--;
                   String s1 = String.valueOf(i);
                   nice_tv.setText(s1);
                   mBean1.setNice(s1);

               }else {
                   nice_iv.setImageResource(R.drawable.ic_heard_true4);
                   mBean1.setNiceFlag(true);
                   String s = nice_tv.getText().toString();
                   int i = Integer.parseInt(s); i++;
                   String s1 = String.valueOf(i);
                   nice_tv.setText(s1);
                   mBean1.setNice(s1);

               }
           }      });

    }

    private void initView() {
        liearlayout_back = (LinearLayout) findViewById(R.id.home_dynamic_back);
        nicelayout = (LinearLayout) findViewById(R.id.home_dynamic_nice);
        mScrollView = (CustomizeScrollView) findViewById(R.id.home_dynamic_srcollview);
        mRcComment = (HotspotRecyclerView) findViewById(R.id.home_dynamic_rc_comment);
        nice_tv = (TextView) findViewById(R.id.home1_dynamic_nice_tv);
        browse_tv = (TextView) findViewById(R.id.home1_item_browse_tv);
        comment_tv = (TextView) findViewById(R.id.home1_dynamic_comment_tv);
        nice_iv = (ImageView) findViewById(R.id.home1_dynamic_nice_iv);
        nice_tv.setText(mBean1.getNice());
        browse_tv.setText(mBean1.getBrowse());
        comment_tv.setText(mBean1.getComment());
        mRcComment.setLayoutManager(new LinearLayoutManager(this));
        mRcComment.setAdapter(mDetailsAdapter);
        //初始化include布局
        View view = findViewById(R.id.home_dynamic_cv_include);
        mCVRcPhoto = (RecyclerView) view.findViewById(R.id.home1_item_dynamic_cv_images);
        mCVcontext = (TextView) view.findViewById(R.id.home1_item_dynamic_cv_context);
        mCVtime = (TextView)view.findViewById(R.id.home1_item_dynamic_cv_time);
        mCVtx = (CircleImageView) view.findViewById(R.id.home1_item_dynamic_cv_tx);
        mCVusername = (TextView) view.findViewById(R.id.home1_item_dynamic_cv_username);
        mCVusername.setText(mBean1.getUsername());
        mCVtx.setImageResource(mBean1.getAvatarImage());
        mCVtime.setText(mBean1.getTime());
        mCVcontext.setText(mBean1.getContext());
        mItemRcAdapter = new HomeItemRcAdapter(this,mBean1.getUrls());
        if(mBean1.getUrls().size()==1){
            mCVRcPhoto.setLayoutManager(new LinearLayoutManager(this));
            mCVRcPhoto.setAdapter(mItemRcAdapter);
        }else{
            mCVRcPhoto.setLayoutManager(new GridLayoutManager(this,3));
            mCVRcPhoto.setAdapter(mItemRcAdapter);
        }

    }

    private void initIntentData() {
        Intent intent = this.getIntent();
        mBean1 = (HomeItemBean1) intent.getSerializableExtra("bean");
        mPosition = intent.getIntExtra("position", 0);
        //评论实现类
        mCommentpersenter = new HomeCommentpersenter(this,this);
        //加载每条评论
        mCommentpersenter.loadCommentData();
    }


    //获取每条评论
    @Override
    public void loadCommentData(ArrayList<HomeCommentBean> homeCommentBeen) {
         mDetailsAdapter = new HomeDynamicDetailsAdapter(homeCommentBeen,this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }
}
